
/*************************************************************************
* Copyright (c) 2008, ShenZhen Coship Electronic Ltd Co.
* All rights reserved.
* 
* File Name:		COSHIPMPEG4Decode.cpp
* Summary:			implementation of the CCOSHIPMPEG4Decode class.
* 
* Current Version:	1.0
* Author(s):
/------------------------------------------------------------------------/
revision			author				reason					date
1.0					Ding Ning			Initialize Version		2008-6-18
/------------------------------------------------------------------------/
*************************************************************************/


#include "stdafx.h"
#include "COSHIPMPEG4Decode.h"

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

//add by li futang 2007-10-07
CCOSHIPMPEG4Decode::CCOSHIPMPEG4Decode()
{
	m_bReversal = TRUE;
	m_hMP4 = NULL;
	m_cType = CODEC_COSHIP_MPEG4;
	m_nWidth = 320;
	m_nHeight = 240;
	m_bInited = FALSE;
	m_pPic = NULL;
}

CCOSHIPMPEG4Decode::~CCOSHIPMPEG4Decode()
{
	SAFE_DELETE(m_pDecBuf);
}

BOOL CCOSHIPMPEG4Decode::Init(const char* pbuf, int len)
{
	//initial the codec
	if(!m_bInited) 
	{
		m_pDecBuf = new DecBuf((CircleBuf*)pbuf);
		m_pDecBuf->CreateObj();
		
		while (m_hMP4 == NULL)
		{
			m_hMP4 = MP4DecodeInitialIH(m_pDecBuf);
		}
		
		MP4DecodeGetInfoIH(m_hMP4, m_nWidth, m_nHeight);
		
		
		m_bInited = TRUE;
	}
	
	return TRUE;
}

void CCOSHIPMPEG4Decode::SetMotion(bool set, int sensitivity, int erosion, int dilation)
{
	if(m_hMP4)
	{
		//set motion detection parameter
		if(set)
		{
			MP4DecodeSetParamIH(m_hMP4,MP4SETCODE_MDEN,(void *)1);
			MP4DecodeSetParamIH(m_hMP4,MP4SETCODE_MDSENTIVITY,(void *)sensitivity);	
			MP4DecodeSetParamIH(m_hMP4,MP4SETCODE_MDEROSION,(void *)erosion);
			MP4DecodeSetParamIH(m_hMP4,MP4SETCODE_MDDILATION,(void *)dilation);
		}
		else
		{
			MP4DecodeSetParamIH(m_hMP4,MP4SETCODE_MDEN,(void *)0);
		}
		//
	}
}

void CCOSHIPMPEG4Decode::GetMotion(void **pparam)
{
	if(m_hMP4)
	{
		MP4DecodeGetParamIH(m_hMP4,MP4GETCODE_MOTIONDECTION,pparam);
	}
}

void CCOSHIPMPEG4Decode::Destroy()
{
	if(m_hMP4)
	{
		MP4DecodeFinishIH(m_hMP4);
		m_hMP4 = NULL;
	}
}

BOOL CCOSHIPMPEG4Decode::Decode(const char* pbuf, int len, int subLen)
{
	m_pPic = NULL;
	
	if((m_pDecBuf->GetCir())->GetStock()>10240)
	{
		m_pPic =  MP4DecodeOneFrameIH(m_hMP4);
		if((int)m_pPic > 0)
			return TRUE;
		else
			return FALSE;
	}
	return TRUE;
}

void CCOSHIPMPEG4Decode::InitPicInfo()
{
	MP4DecodeGetInfoIH(m_hMP4, m_nWidth, m_nHeight);
}
